package com.markerhub.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.entity.SysCollege;
import com.markerhub.entity.SysCourseLocation;
import com.markerhub.entity.SysLocation;
import org.springframework.web.bind.annotation.*;
import com.markerhub.common.lang.Result;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/sys/location")
public class SysLocationController extends BaseController {

    @GetMapping("/list")//获取全部上课地点列表
    public Result list(String username){
        List<SysLocation> pageData = sysLocationService.list( new QueryWrapper<SysLocation>()
                .like(StrUtil.isNotBlank(username), "username", username));
        return Result.succ(pageData);
    }

    @GetMapping("/building")//获取楼栋列表
    public Result building(){
        List<SysLocation> pageData = sysLocationService.list( new QueryWrapper<SysLocation>());
        List<String> s=new ArrayList<>();
        for(SysLocation t:pageData){
            if(!s.contains(t.getBuilding())){
                s.add(t.getBuilding());
            }
        }
        return Result.succ(s);
    }

    @GetMapping("/get")//获取单个信息
    public Result get(@RequestBody SysLocation sysLocation){

        SysLocation sysLocation1=sysLocationService.getById(sysLocation.getId());
        return Result.succ(sysLocation1);
    }

    @PostMapping("/save")//新增一个上课地点
    public Result save(@RequestBody SysLocation sysLocation){
        SysLocation sys =null;
        sys=sysLocationService.getObject(sysLocation.getCourseLocation());
        if(sys!=null){
            return Result.fail("地点已存在");
        }else {
            sysLocationService.save(sysLocation);
            return Result.succ("");
        }
    }

    @PostMapping("/update")//修改上课教室的名字
    public Result update(@RequestBody SysLocation sysLocation){
        //没有修改教室的名字
        if(sysLocation.getCourseLocation().equals(sysLocationService.getLocationById(sysLocation.getId()))){
            //没有修改教室的名字
            sysLocationService.updateById(sysLocation);
            return Result.succ("");

        }else{
            //修改后的名字是否重复
            SysLocation sys =null;
            sys=sysLocationService.getObject(sysLocation.getCourseLocation());
            if(sys!=null){
                return Result.fail("地点已存在");
            }else {
                sysLocationService.updateById(sysLocation);
                return Result.succ("");
            }
        }
    }

    @PostMapping("/delete")//删除一个上课地点
    public Result delete(@RequestBody SysLocation sysLocation){
        Long id= sysLocationService.getObject(sysLocation.getCourseLocation()).getId();
        SysCourseLocation sysCourseLocation=null;
        sysCourseLocation=sysCourseLocationService.getByLocationId(id);

        if(sysCourseLocation == null){
            sysLocationService.removeById(sysLocationService.getIdByLocation(sysLocation.getCourseLocation()));
            return Result.succ("");
        }else{
            return Result.fail("该教室内还有课程");
        }
    }

}
